#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/3/21 11:43
# @Author  : 柳晓飞
# @Site    : 
# @File    : tableManager.py
# @Software: PyCharm
import table
import net.protocal as protocal
import jjGlobal.jjGlobal as jjGlobal
import player
import json

class TableManager:
    def __init__(self):
        self.tablesDict = {}
        self.idlePlayerList = []#已经登陆没有在游戏中的玩家、即队列中的玩家
        self.startTableID = 100001
        self.tablesNum = 3
        for i in range(self.startTableID,self.startTableID+self.tablesNum):
            newTable = table.Table(i)
            newTable.customInit()
            newTable.logic.table = newTable
            self.tablesDict[i] = newTable

    def customInit(self):
        jjGlobal.gameserver.gameServerHand.registMsg(protocal.enterTable, self, self.reqEnterTable)
        jjGlobal.gameserver.gameServerHand.registMsg(protocal.tableInfo, self, self.reqGetTableInfo)

    # 进入牌桌请求
    def reqEnterTable(self,data):
        playerID = data['playerID']
        tableSize = data['tableSize']
        tableID = self.enterIdleList(playerID,tableSize)
        msg = {}
        msg['id'] = protocal.enterTable
        if tableID == None:
            msg['code'] = 1
            print '进入牌桌失败'
        else:
            msg['code'] = 0
            msg['tableID'] = tableID
            msg['pos'] = self.tablesDict[tableID].getPosByPlayerID(playerID)
        d = json.dumps(msg)
        jjGlobal.gameserver.gameServerHand.sendMsg(playerID, d)

    # 获取牌桌信息请求
    def reqGetTableInfo(self, data):
        playerID = data['playerID']
        tableID = data['tableID']#self.getTableIDByPlayerID(playerID)
        msg = {}
        msg['id'] = protocal.tableInfo
        if None == tableID:
            print '根据玩家ID获取牌桌ID失败'
            print 'playerID = %d'%playerID
            msg['code'] = 1
        else:
            msg['code'] = 0
            table = self.tablesDict[tableID]
            newPlayers = {}
            for (pos,p) in table.posDict.items():
                newPlayer = {}
                newPlayer['pos'] = pos
                newPlayer['playerID'] = p.playerID
                newPlayers[pos] = newPlayer
            msg['players'] = newPlayers
        d = json.dumps(msg)
        jjGlobal.gameserver.gameServerHand.sendMsg(playerID, d)

    def enterIdleList(self,playerID,tableSize):
        #self.idlePlayerList.append(player.Player(playerID))
        return  self.enterTable(player.Player(playerID),None,tableSize)

    def leaveIdleList(self,playerID):
        for p in self.idlePlayerList:
            if p.playerID == playerID:
                self.idlePlayerList.remove(p)
                break

    def enterTable(self,p,tableID,tableSize):
        for i in range(self.startTableID, self.startTableID + self.tablesNum):
            tb = self.tablesDict[i]
            if tb.size == None:
                tb.enterPlayer(p)
                tb.setTableSize(tableSize)
                return tb.tableID
            elif tableSize == tb.size and len(tb.posDict) < tb.size:
                tb.enterPlayer(p)
                return tb.tableID
        return None
    def leaveTable(self,playerID):
        for i in range(self.startTableID, self.startTableID + self.tablesNum):
            tb = self.tablesDict[i]
            if tb.leavePlayer(playerID):
                return True
        return False

    # 是否相应客户端发过来的消息
    def isRecMsg(self, data):
        return True

    def getTableIDByPlayerID(self,playerID):
        for table in self.tablesDict.values():
            if table.isEixtPlayer(playerID):
                return table.tableID
        return None





